package com.qc31.monitorlib.ui.carmonitor

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import autodispose2.autoDispose
import com.qc31.baselib.base.LazyFragment
import com.qc31.baselib.custom.timer.CustomDatePicker
import com.qc31.baselib.custom.timer.DateFormatUtils
import com.qc31.baselib.databinding.IncludeSelectSureBinding
import com.qc31.baselib.utils.isFastClick
import com.qc31.baselib.utils.startActivityTo
import com.qc31.baselib.utils.toast
import com.qc31.comlib.Keys
import com.qc31.comlib.TimeUtil
import com.qc31.comlib.adapter.ChooseTimeAdapter
import com.qc31.comlib.databinding.IncludeChooseFastTimeBinding
import com.qc31.comlib.databinding.IncludeChooseTimeBinding
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.FragmentCarLineBinding
import com.qc31.monitorlib.ui.line.HistoryLineInfoActivity

/** @Author Jasper @Time 2020/7/22 @Desc 车辆 历史轨迹 */
class CarHistoryLineFragment :
    LazyFragment<FragmentCarLineBinding>(FragmentCarLineBinding::inflate) {
    private lateinit var mChooseTimeVB: IncludeChooseTimeBinding
    private lateinit var mFastVB: IncludeChooseFastTimeBinding
    private lateinit var mTureVB: IncludeSelectSureBinding
    private val mViewModel: HistoryLineViewModel by viewModels()
    private val mActivityVM by activityViewModels<CarMonitorViewModel>()

    private val mAdapter: ChooseTimeAdapter by lazy { ChooseTimeAdapter() }

    private val mDatePicker: CustomDatePicker by lazy {
        CustomDatePicker(
            requireContext(),
            callback,
            TimeUtil.normalDate,
            mViewModel.nowTimeFormat(),
            true
        ).apply {
            setCanShowPreciseTime(true)
        }
    }

    override fun initData() {
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)

        mFastVB.recyclerWrap.layoutManager = GridLayoutManager(context, 4)
        mFastVB.recyclerWrap.adapter = mAdapter.apply {
            setNewInstance(mViewModel.timeList)
            setOnItemClickListener { _, _, position ->
                index = position
                notifyDataSetChanged()
                mViewModel.chooseTime(position)
            }
        }

        mViewModel.startObserve().autoDispose(scopeProvider).subscribe {
            mChooseTimeVB.lrvStartTime.setRightText(it)
        }

        mViewModel.endObserve().autoDispose(scopeProvider).subscribe {
            mChooseTimeVB.lrvEndTime.setRightText(it)
        }

        mViewModel.historyLineObserver().autoDispose(scopeProvider).subscribe {
            var speed = binding.editThresholdSpeed.text.toString()
            if (speed.isEmpty()) speed = "0"
            startActivityTo<HistoryLineInfoActivity>(requireContext()) {
                putExtra(Keys.INTENT_TITLE, mViewModel.carName)
                putExtra(Keys.INTENT_OTHER, speed)
                putExtra(Keys.INTENT_DATA, mViewModel.params)
            }

        }
    }

    override fun initView(view: View) {
        mChooseTimeVB = IncludeChooseTimeBinding.bind(binding.root)
        mTureVB = IncludeSelectSureBinding.bind(binding.root)
        mFastVB = IncludeChooseFastTimeBinding.bind(binding.root)

        mActivityVM.carId.observe(this) {
            mViewModel.carId = it
        }
        mActivityVM.carName.observe(this) {
            mViewModel.carName = it
        }
        mTureVB.tvSure.postDelayed({
            if (mViewModel.carId.isEmpty()) {
                R.string.hint_toast_choose_car.toast(requireContext())
            }
        }, 200)
    }

    override fun initEvent() {
        mChooseTimeVB.lrvStartTime.rightClick().autoDispose(scopeProvider).subscribe {
            mViewModel.isStart = true
            mDatePicker.show(mChooseTimeVB.lrvStartTime.getRightText())
        }

        mChooseTimeVB.lrvEndTime.rightClick().autoDispose(scopeProvider).subscribe {
            mViewModel.isStart = false
            mDatePicker.show(mChooseTimeVB.lrvEndTime.getRightText())
        }

        mTureVB.tvSure.setOnClickListener {
            if (isFastClick()) return@setOnClickListener
            mViewModel.getHistory(
                mChooseTimeVB.lrvStartTime.getRightText(),
                mChooseTimeVB.lrvEndTime.getRightText(),
                binding.cbFilterStop.isChecked,
                binding.editThresholdSpeed.text.toString(),
                binding.editThresholdStop.text.toString(),
                binding.cbFilterInvalid.isChecked,
                binding.cbFilterAppend.isChecked
            )
        }
    }

    private val callback: CustomDatePicker.Callback = object : CustomDatePicker.Callback {
        override fun onTimeSelected(timestamp: Long) {
            mAdapter.index = -1
            mAdapter.notifyDataSetChanged()
            val time: String = DateFormatUtils.long2Str(timestamp, true)

            if (mViewModel.isStart) {
                mChooseTimeVB.lrvStartTime.setRightText(time)
            } else {
                mChooseTimeVB.lrvEndTime.setRightText(time)
            }
            mDatePicker.dismiss()
        }
    }
}